package com.jixiantubu.luntan;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.jixiantubu.adapter.Luntan_TuPianAdapter;
import com.jixiantubu.bean.Travelsbean;
import com.jixiantubu.bean.TupianBean;
import com.jixiantubu.tupianlunbo.MyPagerAdapter_fx;
import com.jixiantubu.tupianlunbo.ZoomViewPager_fx;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.app.AlertDialog;

public class LuntanTupianActivity extends Activity implements OnClickListener {
	ImageView luntan_tupian_back;
	Luntan_TuPianAdapter adapter;
	List<TupianBean> list = new ArrayList<TupianBean>();
	PullToRefreshGridView gridView;
	private BitmapUtils bitmapUtils;
	String url = "http://" + MyApplication.getIP()
			+ ":8080/Jinnang/tupian_servlet";
	private int number = 1;// 默认第一页
	private List<ZoomViewPager_fx> ziv_view;
	ViewPager viewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.luntan_tupian_gridview);
		ViewUtils.inject(this);
		bitmapUtils = new BitmapUtils(this);
		initViews();
		setgridView();
		initData(url);
		
	}

	private void initViews() {
		gridView = (PullToRefreshGridView) findViewById(R.id.luntan_tupian_gridview);
		luntan_tupian_back = (ImageView) findViewById(R.id.luntan_tupian_back);
		luntan_tupian_back.setOnClickListener(this);
		
	}

	private void initData(String url) {
		initTouch();
		final CustomProgressDialog pd = new CustomProgressDialog(
				LuntanTupianActivity.this, "正在拼命加载数据,请稍等！", R.anim.frame);
		pd.show();
		RequestParams params=new RequestParams();
		params.addBodyParameter("key", "1");
        params.addBodyParameter("page", "1");
        MyApplication.httpclient.configSoTimeout(5000);
        MyApplication.httpclient.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				pd.dismiss();
				MyApplication.show(LuntanTupianActivity.this, "加载数据失败，请检查网络!");
				
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				pd.dismiss();
				String result=arg0.result;
				Log.e("数据库传来的论坛图片", result.toString());
				Gson gson=new Gson();
				Type type = new TypeToken<List<TupianBean>>() {
				}.getType();
				List<TupianBean> list1=gson.fromJson(result, type);
				list.addAll(list1);
				adapter.setList(list);
				Log.e("数据库传来的解析后的论坛图片", result.toString());
				adapter.notifyDataSetChanged();
				
			}
		});
	}

	private void setgridView() {
		// 设置PullToRefreshListView添加上拉和下拉的方式
		gridView.setMode(Mode.BOTH);// 现在是两种都用的情况

		ILoadingLayout freshLayout = gridView
				.getLoadingLayoutProxy(true, false);

		// 修改更新的时间，可以用于提示上次刷新是什么时候
		freshLayout.setLastUpdatedLabel("上次更新2015.10.20");
		// 修改更新的旋转图标
		// freshLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.ic_launcher));
		// 设置下拉的文字
		freshLayout.setPullLabel("下拉刷新");
		// 松开的提示语
		freshLayout.setReleaseLabel("松开刷新");
		// 设置刷新的文字提示
		freshLayout.setRefreshingLabel("正在刷新!请稍等");
		// 设置字体格式
		// freshLayout.setTextTypeface(Typeface.DEFAULT);
		/**
		 * 给PullToRefreshListView添加headView
		 */
		/*HeaderGridView list1 = (HeaderGridView) gridView.getRefreshableView();// 获取PullToRefreshListView中的ListView
		ImageView img = new ImageView(this);
		// img.setImageResource(R.drawable.wo);
		list1.addHeaderView(img);*/

		/**
		 * 为PullToRefreshListView绑定监听器监听用户的下拉和上拉操作
		 */
		gridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {

			@Override
			// 下拉刷新
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// 下拉刷新的逻辑代码


				// 1、清空数据集合中的原有的所有的数据
				list.clear();
				// 2、重新请求第一页数据，存入到数据集合中 3、通知UI展示新数据(notifyDataSetChanged)
				initData(url);
				// 4、通知刷新布局，数据加载完成
				gridView.onRefreshComplete();
			}

			protected void addMoreData() {
				// 刷新之后显示下一页内容，当亲为第一页
				number++;// 页数自加；
				if (0 != adapter.getCount() % 5) {
					Toast.makeText(LuntanTupianActivity.this, "已全部显示",
							Toast.LENGTH_SHORT).show();
				} else {
					String url = "http://" + MyApplication.getIP()
							+ ":8080/Jinnang/tupian_servlet?key=1&page="
							+ number;
					initData(url);

				}
			}

			@Override
			// 上拉加载
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
	
				addMoreData();
				

				gridView.onRefreshComplete();

			}
		});

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.luntan_tupian_back:
			finish();
			break;
		default:
			break;
		}

	}
private void initTouch(){
	adapter = new Luntan_TuPianAdapter(LuntanTupianActivity.this, list,null);
	gridView.setAdapter(adapter);
	gridView.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			final AlertDialog dialog = new AlertDialog.Builder(LuntanTupianActivity.this, R.style.Transparent).create();
			View vp = LayoutInflater.from(dialog.getContext()).inflate(R.layout.view_pager_fx, null);
			viewPager = (ViewPager) vp.findViewById(R.id.zoomViewPager);
			MyPagerAdapter_fx	mp = new MyPagerAdapter_fx(LuntanTupianActivity.this,list);
			viewPager.setAdapter(mp);
			viewPager.setCurrentItem(arg2);
			dialog.show();
			dialog.setContentView(vp);
			
		}
	});	
	adapter.notifyDataSetChanged();
}
}
